﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class DiaDiemDAO
    {
        public static List<string> getTenAll()
        {
            List<string> result = new List<string>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DIADIEMs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                result.Add(ds.TenDiaDiem);
            }
            return result;
        }

        public static List<DiaDiemDTO> getAll()
        {
            List<DiaDiemDTO> result = new List<DiaDiemDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DIADIEMs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                DiaDiemDTO dto = new DiaDiemDTO();

                dto.MaDiaDiem = ds.MaDiaDiem;
                dto.TenDiaDiem = ds.TenDiaDiem;
                
                result.Add(dto);
            }

            return result;
        }

        public static DiaDiemDTO getAlongCode(int maDiaDiem)
        {
            DiaDiemDTO result = new DiaDiemDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DIADIEMs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaDiaDiem == maDiaDiem).Single();

            result.MaDiaDiem = query.MaDiaDiem;
            result.TenDiaDiem = query.TenDiaDiem;

            return result;
        }

        public static void Insert(DiaDiemDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DIADIEM diadiem = new DIADIEM();
            diadiem.TenDiaDiem = _insert.TenDiaDiem;
            diadiem.Deleted = _insert.Deleted;            

            dbRaoVat.DIADIEMs.InsertOnSubmit(diadiem);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maDiaDiem)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DIADIEM _delete = (from n in dbRaoVat.DIADIEMs
                                 where n.MaDiaDiem == maDiaDiem && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(DiaDiemDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DIADIEM _update = (from n in dbRaoVat.DIADIEMs
                               where n.MaDiaDiem == dto.MaDiaDiem && n.Deleted == 0
                               select n).Single();

            _update.TenDiaDiem = dto.TenDiaDiem;
            
            dbRaoVat.SubmitChanges();
        }

        public static int getMa(string tenDiaDiem)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DIADIEMs.Where(dm => dm.Deleted.Equals(0) &&
                dm.TenDiaDiem == tenDiaDiem).Single();

            return query.MaDiaDiem;
        }

        public static string getTen(int maDiaDiem)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DIADIEMs.Where(dm=>
                dm.MaDiaDiem == maDiaDiem).Single();

            return query.TenDiaDiem;
        }
    }
}
